package controllers.bluehat;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.hibernate.ejb.criteria.expression.function.CurrentDateFunction;

import play.Logger;
import play.data.validation.Error;
import play.data.validation.Valid;
import play.i18n.Messages;
import utils.json.data.MessageJson;
import utils.table.DataTable;
import utils.table.DataTable.DataTablesParameters;
import models.AbstractActivity;
import models.Focus;
import models.IdeaRequest;
import models.InformationRequest;
import models.User;

public class FocusController extends BlueHatBaseController {
	
	/**
	 * Renders the form to create or edit a focus.
	 * @param focus Id of the focus.
	 */
	public static void form(Long id) { 
		Focus focus = null;
		if(id != null) focus = Focus.getById(id, null); // TODO validate the focus!
		render(focus);
	}
	
	
	/**
	 * Save the focus.
	 */
	public static void save(Focus focus) {
		User owner = getCurrentUser();
		focus.owner = owner; 
		
		validation.valid(focus);
		if(validation.hasErrors()) {
			params.flash(); // add http parameters to the flash scope
			validation.keep(); // keep the errors for the next request
			form(null);
       } else {
    	   focus.save();
       }
		
	   Dashboard.index();
	}
	
	public static void confirm_delete(Long id) {
		Focus focus = Focus.getById(id, null); 
		render(focus);
	}
	
	public static void delete(Long id, Boolean confirm) {
		if(confirm != null && confirm) {
			Focus focus = Focus.getById(id, null); 
			/*Logger.info("Removing a focus! This action will remove all child relations. This action is executed here!");
			for(InformationRequest infoRequest : focus.informationRequest.iter()) {
				Logger.debug("Now removing information units!");
				infoRequest.informations.delete();
			}
			Logger.debug("Now removing all information requests!");
			focus.informationRequest.delete();*/
			focus.delete();
		}
		Dashboard.index();
	}
	
	/**
	 *  Detail of the focus.
	 * @param id Id of the focus.
	 */
	public static void detail(Long id) {
		Focus focus = Focus.getById(id, null);
		if(!getCurrentUser().equals(focus.owner)) error();
		
		List<InformationRequest> infoOpenList = InformationRequest.findAllOpen(getCurrentDate(), focus.informationRequest);
		List<InformationRequest> infoRemainList = InformationRequest.findAllRemaining(getCurrentDate(), focus.informationRequest);
		List<InformationRequest> infoClosedList = InformationRequest.findAllClose(getCurrentDate(), focus.informationRequest);
		
		List<IdeaRequest> ideaOpenList = IdeaRequest.findAllOpen(getCurrentDate(), focus.ideasRequest);
		List<IdeaRequest> ideaRemainList = IdeaRequest.findAllRemaining(getCurrentDate(), focus.ideasRequest);
		List<IdeaRequest> ideaClosedList = IdeaRequest.findAllClose(getCurrentDate(), focus.ideasRequest);
		
		render(focus, infoOpenList, infoRemainList, infoClosedList, ideaOpenList, ideaRemainList, ideaClosedList);
	}
}
